package com.example.goodpet.activity.Fragment;

import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.View;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;

import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.Photo;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.example.goodpet.R;
import com.example.goodpet.activity.adapter.PetHospitalAdapter;
import com.example.goodpet.base.BaseFragment;
import com.example.goodpet.common.EndlessRecyclerOnScrollListener;
import com.example.goodpet.common.LoadMoreWrapper;
import com.example.goodpet.utils.MyApplication;
import com.example.goodpet.utils.UIUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class HospitalInfoFragment extends BaseFragment {

    private String TAG=this.getClass().getSimpleName();
    //搜索的信息
    private PoiSearch.Query query;
    //搜索结果
    private PoiSearch poiSearch;
    //具体搜索结果
    private ArrayList<PoiItem> mainPois=new ArrayList<>();


    private PetHospitalAdapter adapter;
    private List<DelegateAdapter.Adapter> subAdapters = new LinkedList();
    private RecyclerView rvHospital;
//    int currentPage = 1;

    private VirtualLayoutManager layoutManager;



    @Override
    public View initView() {

        View view = View.inflate(mContext, R.layout.fragment_hospital_info, null);
        rvHospital=view.findViewById(R.id.rv_hospital);

        return view;
    }

    @Override
    public void initData() {
        try {
            initPoi();
        } catch (AMapException e) {
            e.printStackTrace();
        }
    }

    private void initPoi() throws AMapException, com.amap.api.services.core.AMapException {

        Log.e(TAG, "initPoi: "+MyApplication.locationSpecific );
        query = new PoiSearch.Query("", "动物医疗场所",MyApplication.locationSpecific);
        query.setExtensions("all");
        query.setDistanceSort(true);

//        query = new PoiSearchV2.Query("", "动物医疗场所", "郓城县"/*MyApplication.locationSpecific*/);
        //keyWord表示搜索字符串，
        //第二个参数表示POI搜索类型，二者选填其一，选用POI搜索类型时建议填写类型代码，码表可以参考下方（而非文字）
        //cityCode表示POI搜索区域，可以是城市编码也可以是城市名称，也可以传空字符串，空字符串代表全国在全国范围内进行搜索

        query.setPageSize(10);// 设置每页最多返回多少条poiitem
        query.setPageNum(0);//设置查询页码

//        poiSearchV2 = new PoiSearchV2(mContext, query);
        poiSearch = new PoiSearch(mContext, query);
//        poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(MyApplication.latitude,
//                MyApplication.longitude), 20*1000));//设置周边搜索的中心点以及半径
        poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int i) {
                ArrayList<PoiItem> pois = poiResult.getPois();
                Log.e(TAG, "onPoiSearched: ");
                for (PoiItem poiItem : pois) {
                    Log.e(TAG, "onPoiSearched: "+poiItem.getTitle() );
                }
                mainPois=pois;
                initVLayout();
            }

            @Override
            public void onPoiItemSearched(PoiItem poiItem, int i) {

            }
        });

        poiSearch.searchPOIAsyn();
    }


    private void initVLayout() {
        //1.设置总布局管理器
        layoutManager = new VirtualLayoutManager(mContext);
        rvHospital.setLayoutManager(layoutManager);
        //2.设置回收池大小（相同类型的View可以复用，防止多次创建ItemView）
        RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        viewPool.setMaxRecycledViews(0, 10);
        rvHospital.setRecycledViewPool(viewPool);
        //3.逐个设置子模块适配器
        addHospitalAdapter();

        //4.设置总适配器
        DelegateAdapter mainAdapter = new DelegateAdapter(layoutManager, true);
        mainAdapter.setAdapters(subAdapters);
        rvHospital.setAdapter(mainAdapter);
    }

    private void addHospitalAdapter() {
        LinearLayoutHelper layoutHelper = new LinearLayoutHelper();
        adapter = new PetHospitalAdapter(mContext, mainPois);

        LoadMoreWrapper loadMoreWrapper = new LoadMoreWrapper(adapter) {
            @Override
            public LayoutHelper onCreateLayoutHelper() {
                return layoutHelper;
            }
        };
        subAdapters.add(loadMoreWrapper);

        rvHospital.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
            @Override
            public void onLoadMore() {
                loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING);
                query.setPageNum(++loadMoreWrapper.page);//设置查询页码
                poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
                    @Override
                    public void onPoiSearched(PoiResult poiResult, int i) {
                        if (null != poiResult) {
                            mainPois.addAll(poiResult.getPois());
//                            Log.e(TAG, "onPoiSearched: "+mainPois.size() );
                            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_COMPLETE);//有数据
                        } else {
                            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_END);//没有数据
                        }
                    }

                    @Override
                    public void onPoiItemSearched(PoiItem poiItem, int i) {

                    }
                });
                poiSearch.searchPOIAsyn();
            }
        });
    }
}